package com.gewu.pm.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.gewu.pm.R;

/**
 * 筛选栏组件
 * 包含日期筛选和搜索框
 */
public class FilterBar extends LinearLayout {

    private TextView tvDateFilter;
    private EditText etSearch;
    private LinearLayout llDateFilter;

    public FilterBar(Context context) {
        this(context, null);
    }

    public FilterBar(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public FilterBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        // 设置基础属性
//        setOrientation(HORIZONTAL);
//        setGravity(Gravity.CENTER_VERTICAL);
//        setBackgroundResource(R.color.white);
//        int padding = getResources().getDimensionPixelSize(R.dimen.dp_10);
//        int paddingV = getResources().getDimensionPixelSize(R.dimen.dp_5);
//        setPadding(padding, paddingV, padding, paddingV);

        // 加载布局
        LayoutInflater.from(context).inflate(R.layout.layout_filter_bar, this, true);

        // 获取控件
        tvDateFilter = findViewById(R.id.tv_date_filter);
        etSearch = findViewById(R.id.et_search);
        llDateFilter = findViewById(R.id.ll_date_filter);

        // 读取自定义属性
        if (attrs != null) {
            TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.FilterBar);
            
            // 设置日期筛选文字
            String dateFilterText = ta.getString(R.styleable.FilterBar_dateFilterText);
            if (dateFilterText != null) {
                tvDateFilter.setText(dateFilterText);
            }
            
            // 设置搜索框提示
            String searchHint = ta.getString(R.styleable.FilterBar_searchHint);
            if (searchHint != null) {
                etSearch.setHint(searchHint);
            }
            
            ta.recycle();
        }
    }

    /**
     * 获取日期筛选按钮
     */
    public LinearLayout getDateFilterLayout() {
        return llDateFilter;
    }

    /**
     * 获取日期筛选文本控件
     */
    public TextView getDateFilterTextView() {
        return tvDateFilter;
    }

    /**
     * 获取搜索框
     */
    public EditText getSearchEditText() {
        return etSearch;
    }

    /**
     * 设置日期筛选文字
     */
    public void setDateFilterText(String text) {
        if (tvDateFilter != null) {
            tvDateFilter.setText(text);
        }
    }

    /**
     * 设置搜索提示
     */
    public void setSearchHint(String hint) {
        if (etSearch != null) {
            etSearch.setHint(hint);
        }
    }
}

